package com.tbit.uqbike.object.pojo.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.tbit.uqbike.object.pojo.DispatchApplyLog;
import com.tbit.uqbike.object.pojo.DispatchApplyMachine;
import com.tbit.uqbike.webmanager.util.csv.annotations.Csv;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/2/13 19:18
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString
public class DispatchApplyVO {

    private Integer applyId;
    /**
     * 区域名称
     */
    @Csv(name = "运营区域")
    private String accountName;
    /**
     * 车辆调度详情信息
     */
    private DispatchApplyLog dispatchApplyLog;
    /**
     * 调度车辆列表
     */
    private List<DispatchApplyMachine> machines;


    public List<DispatchApplyFlatCSV> flat() {
        if (CollectionUtil.isEmpty(machines)) {
            return Collections.EMPTY_LIST;
        }
        return machines.stream().map(machine -> DispatchApplyFlatCSV.builder()
                                                                    .accountName(accountName)
                                                                    .applyNum(dispatchApplyLog.getApplyNum())
                                                                    .applyTime(dispatchApplyLog.getApplyTime())
                                                                    .applyUserName(dispatchApplyLog.getApplyUserName())
                                                                    .applyUserPhone(dispatchApplyLog.getApplyUserPhone())
                                                                    .handleTime(dispatchApplyLog.getHandleTime())
                                                                    .handleType(Objects.nonNull(dispatchApplyLog.getHandleType())?dispatchApplyLog.getHandleType().getDesc():"")
                                                                    .userCode(machine.getUserCode())
                                                                    .handleMsg(dispatchApplyLog.getHandleMsg())
                                                                    .build()).collect(Collectors.toList());
    }

    public DispatchApplyAggregationCSV aggregation() {
        String userCode = machines.stream().map(DispatchApplyMachine::getUserCode).collect(Collectors.joining(";"));
        Integer stateInt = dispatchApplyLog.getState();
        //0未处理 1审核通过、2驳回
        String state = Objects.nonNull(stateInt) ? stateInt == 0 ? "未处理" : stateInt == 1 ? "审核通过" : "驳回" : "";
        return DispatchApplyAggregationCSV.builder()
                                          .accountName(accountName)
                                          .applyNum(dispatchApplyLog.getApplyNum())
                                          .applyTime(dispatchApplyLog.getApplyTime())
                                          .applyUserName(dispatchApplyLog.getApplyUserName())
                                          .applyUserPhone(dispatchApplyLog.getApplyUserPhone())
                                          .handleTime(dispatchApplyLog.getHandleTime())
                                          .state(state)
                                          .handleType(Objects.nonNull(dispatchApplyLog.getHandleType())?dispatchApplyLog.getHandleType().getDesc():"")
                                          .userCode(userCode)
                                          .handleMsg(dispatchApplyLog.getHandleMsg())
                                          .stockUserName(dispatchApplyLog.getStockUserName())
                                          .applyReason(dispatchApplyLog.getApplyReason())
                                          .build();
    }

}
